//
//  ViewController.m
//  仿系统日历
//
//  Created by 王猛 on 2022/11/16.
//

#import "ViewController2.h"
#import "YearModel.h"
#import "UIColor+WM.h"
#import "YearView.h"
#import "MonthButton.h"

#define IS_IPHONE_X  ([[UIApplication sharedApplication] statusBarFrame].size.height>20 ?YES:NO)
#define Height_StatusBar ((IS_IPHONE_X==YES)?44.0f: 20.0f)
#define Height_StatusDiff ((IS_IPHONE_X==YES)?0.0f: 0.0f)
#define Height_TabBarDiff ((IS_IPHONE_X==YES)?34.0f: 0.0f)
#define Height_NavBar ((IS_IPHONE_X==YES)?88.0f: 64.0f)
#define Height_TabBar ((IS_IPHONE_X==YES)?83.0f: 49.0f)
#define Height_StatusHeight ((IS_IPHONE_X==YES)?20.0f: 0.0f)

#define COLORHEX(hex) [UIColor colorWithHexString:hex]

/***  当前屏幕宽度 */
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
/***  当前屏幕高度 */
#define kScreenHeight  [[UIScreen mainScreen] bounds].size.height
@interface ViewController2 ()
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) NSMutableArray *offestList;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"系统日历";
    self.dataList = [NSMutableArray array];
    self.offestList = [NSMutableArray array];
    [self loadDataAction];
    [self creatUIAction];
}


- (void)creatUIAction {

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setImage:[UIImage imageNamed:@"tianjia"] forState:0];
    [btn addTarget:self action:@selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    btn2.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn2 setTitleColor:[UIColor blackColor] forState:0];
    [btn2 setTitle:@"搜索" forState:0];
    [btn2 addTarget:self action:@selector(button4Action:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:btn],[[UIBarButtonItem alloc] initWithCustomView:btn2]];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, Height_NavBar, kScreenWidth, kScreenHeight - Height_NavBar)];
    scrollView.alwaysBounceVertical = YES;
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    CGFloat height = 0;
    for (YearModel *model1 in self.dataList) {
        [self.offestList addObject:@(height)];
        YearView *view1 = [[YearView alloc] initWithFrame:CGRectMake(20, height, kScreenWidth-40, 60) withYearModel:model1];
        view1.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:view1];
        
        CGFloat BW = (view1.frame.size.width - 12*2)/3.0;
        CGFloat BH = 30+20*6;
        for (int month = 0; month < 12; month++) {
            MonthModel *model2 = model1.monthList[month];
            MonthButton *btn = [[MonthButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(view1.frame) + (BW + 12) * (month % 3),CGRectGetMaxY(view1.frame) + 20 + (BH + 12)*(month / 3) , BW, BH) withMonthModel:model2];
            [btn addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:btn];
        }
        height = CGRectGetMaxY(view1.frame) + 20 + (BH + 12)*4;
    }
    
    scrollView.contentSize = CGSizeMake(kScreenWidth, height + 50);
    
}

- (void)button4Action:(UIButton *)btn {
    NSLog(@"搜索");
}

- (void)button3Action:(UIButton *)btn {
    NSLog(@"添加");
}

- (void)button2Action:(MonthButton *)btn {
    NSLog(@"点击了%ld月份%ld年",(long)btn.monthModel.month,(long)btn.monthModel.year);
}


- (void)loadDataAction {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *com = [NSDateComponents new];
    if (YES) {
        //初始化第一天 从去年开始
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:[NSDate date]];
        com.year = components.year - 1;
        com.month = 1;
        com.day = 1;
        NSDateComponents *tempCom = [NSDateComponents new];
        [tempCom setValue:0 forComponent:NSCalendarUnitDay];
        NSDate *tempDate = [calendar dateByAddingComponents:tempCom toDate:[calendar dateFromComponents:com] options:0];
        com = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:tempDate];
    }
    
    for (int i = 0; i < 20; i++) {
        YearModel *yearModel = [YearModel new];
        yearModel.monthList = [NSMutableArray array];
        yearModel.year = com.year;
        for (int j = 0; j < 12; j++) {
            MonthModel *monthModel = [MonthModel new];
            monthModel.dayList = [NSMutableArray array];
            monthModel.year = com.year;
            monthModel.month = com.month;
            for (int k = 0; k < 31; k++) {
                DayModel *dayModel = [DayModel new];
                dayModel.day = com.day;
                dayModel.date = com;
                [monthModel.dayList addObject:dayModel];
                // 明天
                NSDateComponents *nextCom = [NSDateComponents new];
                [nextCom setValue:+1 forComponent:NSCalendarUnitDay];
                
                NSDate *nextDate = [calendar dateByAddingComponents:nextCom toDate:[calendar dateFromComponents:com] options:0];
                com = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nextDate];
                
                if (monthModel.month != com.month) {
                    // 下个月
                    break;
                }
            }
            [yearModel.monthList addObject:monthModel];
        }
        [self.dataList addObject:yearModel];
    }
}




@end
